package com.rbs.controller;

import com.rbs.entity.Phone;
import com.rbs.service.Impl.PhoneServiceImpl;
import com.rbs.service.PhoneService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;

@WebServlet(urlPatterns = "/AddPhoneController")
public class AddPhoneController extends HttpServlet {
    private PhoneService phoneService = new PhoneServiceImpl();
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //接收客户端参数
        String name = request.getParameter("phoneName");
        String strPrice = request.getParameter("price");
        BigDecimal price = BigDecimal.valueOf(Double.valueOf(strPrice));
        String strState = request.getParameter("state");
        int state = Integer.parseInt(strState);
        String phone = request.getParameter("phone");
        int brand = Integer.parseInt(phone);
        //将参数封装为对象
        Phone phone1 = new Phone();
        phone1.setPname(name);
        phone1.setPrice(price);
        phone1.setState(state);
        phone1.setBrandId(brand);
        int i = phoneService.insertPhone(phone1);
        response.sendRedirect(request.getContextPath()+"/FindAllPhoneController");
    }
}
